package com.sibu.yx.promotion.web.util;

import com.sibu.yx.promotion.web.support.filter.TokenUidSubject;
import org.apache.shiro.SecurityUtils;

import com.doubo.security.subject.Subject;
import com.doubo.security.util.ThreadContext;


/**
 *
 * @author huminghe
 * @create 2017/8/30
 */
public class SessionUtil {
    /**
     * 获取当前登录信息
     *
     * @return
     */
    public static TokenUidSubject getLoginInfo() {
        Subject subject = ThreadContext.getSubject();
        if (subject != null && subject.isAuthenticated()) {
            TokenUidSubject tokenUidSubject = (TokenUidSubject) subject;
            return tokenUidSubject;
        }
        return null;
    }

    /**
     * 获取当前token
     * @return
     */
    public static String getLoginToken() {
        TokenUidSubject loginInfo = getLoginInfo();
        if (loginInfo.isAuthenticated()) {
            return loginInfo.getToken();
        }
        return null;
    }

    /**
     * 获取登陆平台号
     * @return
     */
    public static int getLoginPlatCode() {
        TokenUidSubject loginInfo = getLoginInfo();
        if (loginInfo.isAuthenticated()) {
            return loginInfo.getLoginPlat().getCode();
        }
        return -1;
    }

    /*
     * @description: 获前当前登陆的用户Id
     * @param: []
     * @return: java.lang.String
     * @author: yuwenbo@wljs.com
     * @date:2020-06-05
     * @time:16:21
     **/
    public static String getCurrentUserId(){
        String userId="";

        org.apache.shiro.subject.Subject subject= SecurityUtils.getSubject();
        if(subject.isAuthenticated()){
            return subject.getPrincipal().toString();
        }
        return userId;
    }
}
